package ldkxingzhe.androidmedia.chapter1;

import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import ldkxingzhe.androidmedia.R;

/**
 * Created by ke on 15-10-26.
 */
public class MediaStoreGalleryActivity extends AppCompatActivity{
    private final String TAG = MediaStoreGalleryActivity.class.getSimpleName();

    public final static int DISPLAY_WIDTH = 200;
    public final static int DISPLAY_HEIGHT = 200;

    private TextView titleTextView;
    private ImageButton imageButton;
    private Cursor cursor;
    private Bitmap bitmap;
    private String imageFilePath;
    private int fileColumn;
    private int titleColumn;
    private int displayColumn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chapter1_activity_mediastore_gallery);
        titleTextView = (TextView)findViewById(R.id.image_title);
        imageButton = (ImageButton)findViewById(R.id.image_button);

        String[] columns = {MediaStore.Images.Media.DATA,
                MediaStore.Images.Media._ID,
                MediaStore.Images.Media.TITLE,
                MediaStore.Images.Media.DISPLAY_NAME};
        cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, null);
        fileColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        titleColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.TITLE);
        displayColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);

        if(cursor.moveToFirst()){
            titleTextView.setText(cursor.getString(titleColumn));
            imageFilePath = cursor.getString(fileColumn);
            bitmap = getBitmap(imageFilePath);
            imageButton.setImageBitmap(bitmap);
            imageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(cursor.moveToNext()){
                        titleTextView.setText(cursor.getString(titleColumn));
                        imageFilePath = cursor.getString(fileColumn);
                        bitmap = getBitmap(imageFilePath);
                        imageButton.setImageBitmap(bitmap);
                    }
                }
            });
        }
    }

    private Bitmap getBitmap(String filePath){
        BitmapFactory.Options bmpOptions = new BitmapFactory.Options();
        bmpOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, bmpOptions);
        int heightRadio = (int)Math.ceil(bmpOptions.outHeight/DISPLAY_HEIGHT);
        int widthRadio = (int)Math.ceil(bmpOptions.outWidth/DISPLAY_WIDTH);
        if(heightRadio > 1 && widthRadio > 1){
            if(heightRadio >= widthRadio){
                bmpOptions.inSampleSize = heightRadio;
            }else{
                bmpOptions.inSampleSize = widthRadio;
            }
        }
        bmpOptions.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, bmpOptions);
    }
}
